package com.lanou.dllo.findfood.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.AuthFailureError;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.baoyz.widget.PullRefreshLayout;
import com.google.gson.Gson;

import com.lanou.dllo.findfood.adpter.NewsRecyerAdpter;
import com.lanou.dllo.findfood.R;
import com.lanou.dllo.findfood.dialog.CookProgressDialog;
import com.lanou.dllo.findfood.entity.NewList;
import com.lanou.dllo.findfood.entity.NewResult;
import com.lanou.dllo.findfood.volley.VolleySington;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dllo on 15/10/26.
 */
public class NewestFragment  extends Fragment{
    private RecyclerView fragmentNewView;
    private NewsRecyerAdpter adpter;
    private PullRefreshLayout newRefresh;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_news,null);
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        requestData();
    }
  private  void   initView(){
      fragmentNewView = (RecyclerView) getActivity().findViewById(R.id.fragmentNewView);


      GridLayoutManager manager = new GridLayoutManager(getContext(),1);
      adpter = new NewsRecyerAdpter();
      fragmentNewView.setLayoutManager(manager);
      fragmentNewView.setAdapter(adpter);

      //下拉刷新
      newRefresh= (PullRefreshLayout) getActivity().findViewById(R.id.newRefresh);
      newRefresh.setRefreshStyle(PullRefreshLayout.STYLE_WATER_DROP);
      newRefresh.setOnRefreshListener(new PullRefreshLayout.OnRefreshListener() {
          @Override
          public void onRefresh() {
              newRefresh.postDelayed(new Runnable() {
                  @Override
                  public void run() {
                      //  requestData();
                      newRefresh.setRefreshing(false);
                  }
              }, 4000);

          }
      });
  }
    private void requestData(){
        final CookProgressDialog acitivity_anmition  = new CookProgressDialog(getContext(),R.id.animaImg);
        acitivity_anmition.show();

       String path = "http://api.wecook.cn/v3/cooking/re" +
               "commend?timestamp=1445691784861&sign=75be8222607f22119fcf39e0196c6ebe&uid=502685&sort=2&wid=f06f82c3d0f511b0d7c1bd250e194106&page=1&batch=20&net=wifi&access=259eea423dee18c7b865b0777cd657cc";
      //  String path = "http://api.wecook.cn/v3/cooking/recommend?timestamp=1445691784843&sign=75be8222607f22119fcf39e0196c6ebe&uid=502685&sort=1&wid=f06f82c3d0f511b0d7c1bd250e194106&page=1&batch=20&net=wifi&access=259eea423dee18c7b865b0777cd657cc";
        JsonObjectRequest request = new JsonObjectRequest(path, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {

                if (response!= null && response.has("result"));

                try {
                    JSONObject result = response.getJSONObject("result");

                    Gson gson = new Gson();
                    NewResult results = gson.fromJson(result.toString(),NewResult.class);
                    if (results != null ){
                        List<NewList> lists = results.getList();
                       adpter.addData(lists);
                    }

                    acitivity_anmition.dismiss();




                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        }){
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                map.put("Accept-Encoding", "");
                return map;
            }
        };

        VolleySington.getInstance().addRequest(request);
    }
}
